package com.beiye.drivertransport.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.frame.adapter.CommonAdapter;
import com.android.frame.adapter.ViewHolder;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.bean.CourseDetailSubjectBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.utils.LogUtils;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.view.LiumRadioGroup;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by ky on 2018/1/13.
 */

public class CourseDetailSubjectApt extends CommonAdapter<CourseDetailSubjectBean.RowsBean> {
    private final List<CourseDetailSubjectBean.RowsBean> mList;
    private Context context;
    private RadioButton radioButtonA;
    private RadioButton radioButtonB;
    private RadioButton radioButtonC;
    private RadioButton radioButtonD;
    private RadioButton radioButtonE;
    private RadioButton radioButtonF;
    private LinearLayout checkbox_ll;
    private CheckBox checkBoxA,checkBoxB,checkBoxC,checkBoxD,checkBoxE,checkBoxF;
    //  private HashMap<String,Boolean> states=new HashMap<String,Boolean>();//记录所有radiobutton被点击的状态

    private LiumRadioGroup radioGroup;

    public CourseDetailSubjectApt(Context context, List<CourseDetailSubjectBean.RowsBean> mList, int itemLayoutId) {
        super(context, mList, itemLayoutId);
        this.context = context;
        this.mList = mList;


    }

    @Override
    public void convert(ViewHolder holder, final CourseDetailSubjectBean.RowsBean item, final int position) {
        TextView tv_course_title = holder.getView(R.id.tv_course2);
        ImageView img_course_img = holder.getView(R.id.img_course_item);
        tv_course_title.setText(position + 1 + ". " + mList.get(position).getQuestionDesc());


        if (mList.get(position).getUrl() == null) {
            img_course_img.setVisibility(View.GONE);

        } else {
            img_course_img.setVisibility(View.VISIBLE);

            if (mList.get(position).getUrl().contains("aliyuncs.com")) {
                Picasso.with(context).load(Uri.parse(mList.get(position).getUrl())).placeholder(R.mipmap.no_data2).into(img_course_img);
            } else {

                Picasso.with(context).load(Uri.parse(AppInterfaceConfig.getRequestUrl(mList.get(position).getUrl()))).placeholder(R.mipmap.no_data2).into(img_course_img);
            }
        }

        checkbox_ll = (LinearLayout) holder.getView(R.id.checkbox_ll);
        checkBoxA=(CheckBox)checkbox_ll.findViewById(R.id.checkbox_course0);
        checkBoxB=(CheckBox)checkbox_ll.findViewById(R.id.checkbox_course1);
        checkBoxC=(CheckBox)checkbox_ll.findViewById(R.id.checkbox_course2);
        checkBoxD=(CheckBox)checkbox_ll.findViewById(R.id.checkbox_course3);
        checkBoxE=(CheckBox)checkbox_ll.findViewById(R.id.checkbox_course4);
        checkBoxF=(CheckBox)checkbox_ll.findViewById(R.id.checkbox_course5);
        radioGroup = (LiumRadioGroup) holder.getView(R.id.course_rg);
        radioButtonA = (RadioButton) radioGroup.findViewById(R.id.radioButton_course);
        radioButtonB = (RadioButton) radioGroup.findViewById(R.id.radioButton_course1);
        radioButtonC = (RadioButton) radioGroup.findViewById(R.id.radioButton_course2);
        radioButtonD = (RadioButton) radioGroup.findViewById(R.id.radioButton_course3);
        radioButtonE = (RadioButton) radioGroup.findViewById(R.id.radioButton_course4);
        radioButtonF = (RadioButton) radioGroup.findViewById(R.id.radioButton_course5);

        CheckBox[] checkBoxs=new CheckBox[]{checkBoxA,checkBoxB,checkBoxC,checkBoxD,checkBoxE,checkBoxF};
        RadioButton[] radioButtons=new RadioButton[]{radioButtonA,radioButtonB,radioButtonC,radioButtonD,radioButtonE,radioButtonF};
        String[] options=new String[]{mList.get(position).getOptionA(),mList.get(position).getOptionB(),mList.get(position).getOptionC(),mList.get(position).getOptionD(),mList.get(position).getOptionE(),mList.get(position).getOptionF()};
        String[] numLables=new String[]{"A","B","C","D","E","F"};

        if(mList.get(position).getqType()==2){
            checkbox_ll.setVisibility(View.VISIBLE);
            radioGroup.setVisibility(View.GONE);
            buttonShow(checkBoxs,options,numLables);
        }else{
            checkbox_ll.setVisibility(View.GONE);
            radioGroup.setVisibility(View.VISIBLE);
            buttonShow(radioButtons,options,numLables);
        }

        List<String> answers=new ArrayList<>();
        for(int i=0;i<checkBoxs.length;i++){
            int finalI = i;
            checkBoxs[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!checkBoxs[finalI].isChecked()){
                        answers.remove(numLables[finalI]);
                        if(answers.size()<=0){
                            item.setSelect(false);//是否选择
                        }
                    }else{
                        item.setSelect(true);
                        answers.add(numLables[finalI]);
                        if(!mList.get(position).getRightOption().contains(numLables[finalI])){
                            showToastDialog(numLables[finalI]);
                            checkBoxs[finalI].setChecked(false);
                            answers.remove(numLables[finalI]);
                        }
                    }
                    LogUtils.e("测试", "CourseDetailSubjectApt: "+listToStr(answers) );
                    //判断答案是否正确
                    if(mList.get(position).getRightOption().equals(listToStr(answers))){
                        item.setLOOP(true);
                    }else{
                        item.setLOOP(false);
                    }
                }
            });
        }

        /*if (mList.get(position).getOptionA() == null) {
            radioButtonA.setVisibility(View.GONE);

        } else {
            radioButtonA.setVisibility(View.VISIBLE);
            radioButtonA.setText("A." + mList.get(position).getOptionA());
        }
        if (mList.get(position).getOptionB() == null) {
            radioButtonB.setVisibility(View.GONE);

        } else {
            radioButtonB.setVisibility(View.VISIBLE);
            radioButtonB.setText("B." + mList.get(position).getOptionB());
        }

        if (mList.get(position).getOptionC() == null) {
            radioButtonC.setVisibility(View.GONE);

        } else {
            radioButtonC.setVisibility(View.VISIBLE);
            radioButtonC.setText("C." + mList.get(position).getOptionC());
        }


        if (mList.get(position).getOptionD() == null) {
            radioButtonD.setVisibility(View.GONE);

        } else {
            radioButtonD.setVisibility(View.VISIBLE);
            radioButtonD.setText("D." + mList.get(position).getOptionD());
        }*/
        radioGroup.setOnCheckedChangeListener(new LiumRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(LiumRadioGroup group, int checkedId) {
                item.setSelect(true);

                if (checkedId == R.id.radioButton_course) {
                    String right_A = mList.get(position).getRightOption();
                    if ("A".equals(right_A)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("A");
                    }
                } else if (checkedId == R.id.radioButton_course1) {
                    String right_B = mList.get(position).getRightOption();
                    if ("B".equals(right_B)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("B");
                    }
                } else if (checkedId == R.id.radioButton_course2) {
                    String right_C = mList.get(position).getRightOption();
                    if ("C".equals(right_C)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("C");
                    }
                } else if (checkedId == R.id.radioButton_course3) {
                    String right_D = mList.get(position).getRightOption();
                    if ("D".equals(right_D)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("D");
                    }
                } else if (checkedId == R.id.radioButton_course4) {
                    String right_E = mList.get(position).getRightOption();
                    if ("E".equals(right_E)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("E");
                    }
                } else if (checkedId == R.id.radioButton_course5) {
                    String right_F = mList.get(position).getRightOption();
                    if ("F".equals(right_F)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("F");
                    }
                }


            }
        });
    }

    private void buttonShow(Button[] buttons, String[] options, String[] labels) {
        for(int i=0;i<buttons.length;i++){
            if (options[i] == null || options[i].equals("")) {
                buttons[i].setVisibility(View.GONE);
            } else {
                buttons[i].setVisibility(View.VISIBLE);
                buttons[i].setText(labels[i] +". "+ options[i]);
            }
        }
    }

    private void showToastDialog(String str){
        TiShiDialog.Builder builder1 = new TiShiDialog.Builder(context);
        builder1.setMessage(str+"不是正确答案");
        builder1.setTitle("提示:");
        builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder1.create().show();
    }

    private String listToStr(List<String> lists){
        String str="";
        Collections.sort(lists);
        for(int i=0;i<lists.size();i++){
            str+=lists.get(i);
        }
        return str;
    }
}